package com.gxuwz.cyhschool;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class recharge extends AppCompatActivity implements View.OnClickListener, TextWatcher {
    private Button  btn_10, btn_20, btn_30, btn_50, btn_100, btn_recharge;

    private TextView tv_cz;
    private EditText ed_money;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);

        getSupportActionBar().hide();

        btn_10 = findViewById(R.id.btn_10);
        btn_20 = findViewById(R.id.btn_20);
        btn_30 = findViewById(R.id.btn_30);
        btn_50 = findViewById(R.id.btn_50);
        btn_100 = findViewById(R.id.btn_100);

        btn_recharge = findViewById(R.id.btn_recharge);
        tv_cz = findViewById(R.id.tv_cz);
        ed_money = findViewById(R.id.ed_money);


        btn_10.setOnClickListener(this);
        btn_20.setOnClickListener(this);
        btn_30.setOnClickListener(this);
        btn_50.setOnClickListener(this);
        btn_100.setOnClickListener(this);

        btn_recharge.setOnClickListener(this);
        ed_money.addTextChangedListener(this);
        btn_10.performClick();


        ImageView imageView=findViewById(R.id.iv_return);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        String start = "充值(";
        String end = ")";
        String money = "";
        Button button = (Button) v;
        if (v.getId() != R.id.btn_recharge) {
            money = button.getText().toString();
        }
        switch (v.getId()) {
            case R.id.iv_return:
                this.finish();
                return;
            case R.id.btn_recharge:
                Intent intent = new Intent();
                money = button.getText().toString();
                money = money.substring(3, money.length() - 2);
                intent.setClass(recharge.this, cashier.class);
                intent.putExtra("money", money);
                startActivity(intent);
                return;
        }

        btn_recharge.setText(start + money + end);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        String start = "充值(";
        String end = "元)";
        String money = s.toString();
        if (Double.parseDouble(money) > 1000 || Double.parseDouble(money) < 0) {
            Toast.makeText(recharge.this, "充值金额为1000元以内", Toast.LENGTH_SHORT).show();
            return;
        }
        btn_recharge.setText(start + money + end);
    }

}